package com.core.exception;

import com.utility.exception.ServiceException;
import com.utility.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.UnexpectedTypeException;
import java.sql.SQLException;

/**
 * @Author hewei
 * @Description //TODO  自定义异常捕捉
 * @Date 17:29 2021/5/11
 **/
@RestControllerAdvice
@Slf4j
public class ExceptionController {



    /**
     * 事务添加失败异常（自定义异常）
     * @param ex
     * @return
     */
    @ExceptionHandler(ServiceException.class)
    @ResponseStatus(HttpStatus.OK)
    public Result globalServiceException(ServiceException ex) {
        log.error(ex.getMessage());
        return  Result.returnCode(-1,ex.getMessage());
    }


    /**
     * 事务添加失败异常（自定义异常）
     * @param ex
     * @return
     */
    @ExceptionHandler(UnexpectedTypeException.class)
    @ResponseStatus(HttpStatus.OK)
    public Result globalServiceException(UnexpectedTypeException ex) {
        log.error(ex.getMessage());
        return  Result.returnCode(-1,ex.getMessage());
    }


    /**
     * 权限异常：权限不足不允许访问
     * @param ex
     * @return
     */
//    @ExceptionHandler(AccessDeniedException.class)
//    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
//    public Result globalServiceException(AccessDeniedException ex) {
//        log.error(ex.getMessage());
//        return  Result.returnCode(-1,"无权访问。");
//    }


    /**
     * 安全认证自定义异常
     * @param ex
     * @return
     */
//    @ExceptionHandler(AuthenticationCustomException.class)
//    @ResponseStatus(HttpStatus.UNAUTHORIZED)
//    public Result globalServiceException(AuthenticationCustomException ex) {
//        log.error(ex.getMessage());
//        return  Result.returnCode(-1,ex.getMessage());
//    }



    /**
     * 实体类判断注解失败异常（自定义异常）
     * @param ex
     * @return
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseStatus(HttpStatus.OK)
    public Result globalMethodArgumentNotValidException(MethodArgumentNotValidException ex) {
        log.error(ex.getMessage());
        return  Result.returnCode(-1,ex.getMessage().substring(ex.getMessage().lastIndexOf("message")+9, ex.getMessage().length()-3));
    }


    /**
     * 实体类判断注解失败异常（自定义异常）
     * @param ex
     * @return
     */
    @ExceptionHandler(SQLException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Result globalSqlExceptionm(SQLException ex) {
        log.error(ex.getMessage());
        ex.printStackTrace();
        return  Result.returnCode(-1,ex.getMessage());
    }


    /**
     * NullPointerException
     * @param ex
     * @return
     */
    @ExceptionHandler(NullPointerException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Result globalNullPointerException(NullPointerException ex) {
        ex.printStackTrace();
        log.error(getClass().getName()+"异常类型： NullPointerException 空指针异常信息");
        return  Result.returnCode(-1,"空指针异常");
    }

    /**
     * 所有异常
     * @param ex
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Result sumException(Exception ex) {
        log.error(ex.getMessage());
        ex.printStackTrace();
        return  Result.returnCodeError(ex.getMessage());
    }

}
